﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.DAL.RecordCenter;
using System.Linq;
using System.Collections;

namespace MS.InformationManagement.Records.ObjectDef.Receivers.BackdropEventReceiver
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class BackdropEventReceiver : SPItemEventReceiver
    {
        /// <summary>
        /// An item is being deleted.
        /// </summary>
        public override void ItemDeleting(SPItemEventProperties properties)
        {
            base.ItemDeleting(properties);
            if (properties.List.Title.Equals("Fondos"))
            {
                try
                {
                    this.EventFiringEnabled = false;
                    //if (!properties.Web.CurrentUser.IsSiteAdmin)
                    //{
                        properties.Cancel = true;
                        properties.ErrorMessage = string.Format("No es posible eliminar items de esta lista"
                            , properties.Web.CurrentUser.LoginName);
                    //}
                }
                catch (Exception ex)
                {
                    Logger.LogException(ex, Microsoft.SharePoint.Administration.EventSeverity.Error,
                         SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
                    throw;
                }
                finally { this.EventFiringEnabled = true; }
            }
        }

        public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
            try
            {
                bool backdropExists = false;
                this.EventFiringEnabled = false;
                using (DataContext ctx = new DataContext(properties.Web.Url))
                {
                    backdropExists = ctx.Fondos.Count(f => f.FondoAlQuePertenece == null) > 0;
                }

                if (backdropExists && properties.ListItem[CustomSiteColumns.FIELD_BACKDROP_PARENT] == null)
                {
                    properties.Cancel = true;
                    properties.ErrorMessage = "Ya existe un fondo en la plataforma";
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, Microsoft.SharePoint.Administration.EventSeverity.Error,
                          SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
                throw;
            }
            finally { this.EventFiringEnabled = true; }
        }

        /// <summary>
        /// An item is being updated.
        /// </summary>
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
            if (properties.List.Title.Equals("Fondos"))
            {

                try
                {
                    bool afterProperties = false;
                    bool beforeProperties = false;

                    foreach (var prop in properties.AfterProperties)
                        afterProperties = true;

                    foreach (var prop in properties.BeforeProperties)
                        beforeProperties = true;

                    if (afterProperties || beforeProperties)
                    {

                        this.EventFiringEnabled = false;
                        properties.ErrorMessage = string.Format("No es permitido modificar un fondo una vez creado");
                        properties.Status = SPEventReceiverStatus.CancelWithError;
                        properties.Cancel = true;
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogException(ex, Microsoft.SharePoint.Administration.EventSeverity.Error,
                            SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
                    throw ex;
                }
                finally
                {
                    this.EventFiringEnabled = true;
                }
            }
        }

    
    
    }
}
